<?php
require dirname ( dirname ( __FILE__ ) ) . '/../bootstrap.php';
header ( "Content-type: image/" . $type );
/**
 * 验证码
 *
 * @author  rice
 * @copyright  2007 alliance_loan163, Inc.
 * @version  1.0
 * @create date 2008/12/18
 *
 */
function randstr($len = 6, $format = "ALL") {
	switch ($format) {
		case "ALL" :
			$chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
			break;
		case "CHAR" :
			$chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
			break;
		case "NUMBER" :
			$chars = "0123456789";
			break;
			$chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
	}
	$string = "";
	while ( strlen ( $string ) < $len ) {
		$string .= substr ( $chars, mt_rand () % strlen ( $chars ), 1 );
	}
	return $string;
}

$type = "gif";
$width = 50;
$height = 20;

srand ( ( double ) microtime () * 1000000 );
$randval = randstr ( 5, "CHAR" );
sess_add ( '__CAPTCHA__', strtolower ( $randval ) );
sess_add ( "__CAPTCHA__TIMEOUT__", time () + 120 );

if ($type != "gif" && function_exists ( "imagecreatetruecolor" )) {
	$im = @imagecreatetruecolor ( $width, $height );
} else {
	$im = @imagecreate ( $width, $height );
}
$r = array (225, 240, 250, 255 );
$g = array (225, 240, 250, 255 );
$b = array (225, 240, 250, 255 );
$key = rand ( 0, 3 );
$backColor = imagecolorallocate ( $im, 85, 190, 255 ); //$r[$key], $g[$key], $b[$key] );
$borderColor = imagecolorallocate ( $im, 255, 255, 255 );
$pointColor = imagecolorallocate ( $im, 255, 170, 255 );
@imagefilledrectangle ( $im, 0, 0, $width - 1, $height - 1, $backColor );
@imagerectangle ( $im, 0, 0, $width - 1, $height - 1, $borderColor );
$stringColor = imagecolorallocate ( $im, 100, 55, 255 );
$i = 0;
for(; $i <= 100; ++ $i) {
	$pointX = rand ( 2, $width - 2 );
	$pointY = rand ( 2, $height - 2 );
	@imagesetpixel ( $im, $pointX, $pointY, $pointColor );
}
@imagestring ( $im, 4, 5, 1, $randval, $stringColor );
$ImageFun = "Image" . $type;
$ImageFun ( $im );
@imagedestroy ( $im );
?>